package com.CTAtracker.android.Activities;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Models.ColorStation;
import com.CTAtracker.android.Models.Station;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class CTAMapActivity extends Activity {
	/**
	 * Google map to instantiate
	 */
	private GoogleMap _gMap = null; 
	/**
	 * Log 
	 */
	private static final String TAG = "CTAMapActivity";
	/**
	 * Center point to open the map with 
	 */
	static final LatLng JACKSON_RED = new LatLng(41.878153, -87.627596);
	/**
	 * Data Source to retrieve the stations position  
	 */
	private StationDataSource _stationDataSource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
		if (result == ConnectionResult.SUCCESS){
			setContentView(R.layout.map_activity);

			// Inflate Menu
			ActionBar actionbar = getActionBar();
			actionbar.setDisplayShowTitleEnabled(false);
			actionbar.setDisplayShowCustomEnabled(true);
			actionbar.setDisplayShowHomeEnabled(false);
			actionbar.setCustomView(R.layout.actionbar_back);

			ImageView back_button = (ImageView) findViewById(R.id.back_button);
			back_button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});

			_stationDataSource = new StationDataSource(this);

			this.actionMap();

		}else {
			switch (result){
			case ConnectionResult.SERVICE_MISSING:
				Log.wtf(TAG, "Service google play missing");
				break; 
			case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
				Log.wtf(TAG, "The service version need to be updated");
				break;
			case ConnectionResult.SERVICE_DISABLED:
				Log.wtf(TAG, "The service is disable");
				break; 
			} 
			GooglePlayServicesUtil.getErrorDialog(result, this, Activity.RESULT_CANCELED);
		}

	}

	private void actionMap(){
		_gMap = ((MapFragment)(getFragmentManager().findFragmentById(R.id.map))).getMap();
		_gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(JACKSON_RED, 20));
		_gMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

		this.addAllMark();
		this.addLines();
	}

	private void addAllMark() {
		_stationDataSource.open();
		ArrayList<Station> listStation = _stationDataSource.getAllStation();
		_stationDataSource.close();
		for (int i=0; i<listStation.size(); i++){
			addMark(listStation.get(i));
		}
	}

	private void addMark(Station station){
		LatLng latLng = station.getLatLng();
		_stationDataSource.open();
		boolean isUnique = _stationDataSource.isUnique(station.get_parent_stop_id());
		_stationDataSource.close();
		int resourceId = R.drawable.gaymappin;
		if(isUnique){
			switch(station.get_line_id()-1){
			case ColorStation.RED:
				resourceId = R.drawable.redmappin;
				break;
			case ColorStation.BLUE:
				resourceId = R.drawable.bluemappin;
				break;
			case ColorStation.GREEN:
				resourceId = R.drawable.greenmappin;
				break;
			case ColorStation.BROWN:
				resourceId = R.drawable.brownmappin;
				break;
			case ColorStation.YELLOW:
				resourceId = R.drawable.yellowmappin;
				break;
			case ColorStation.ORANGE:
				resourceId = R.drawable.orangemappin;
				break;
			case ColorStation.PINK:
				resourceId = R.drawable.pinkmappin;
				break;
			case ColorStation.PURPLE:
				resourceId = R.drawable.purplemappin;
				break;
			default: 
				resourceId = R.drawable.gaymappin;
				break;
			}
		}
		_gMap.addMarker(new MarkerOptions()
		.position(latLng).title(station.get_name())
		.icon(BitmapDescriptorFactory.fromResource(resourceId)));
	}

	private void addLines(){
		for (int i=0; i<8; i++){
			addLine(i);
		}
	}
	
	private void addLine(int idLine){
		ArrayList<Station> listStation = new ArrayList<Station>();
		_stationDataSource.open(); 
		listStation = _stationDataSource.getListStationByLine(idLine+1);
		_stationDataSource.close();
		
		PolylineOptions rectOptions = new PolylineOptions();
		
		for(int i=0; i<listStation.size(); i++ ){
			rectOptions.add(listStation.get(i).getLatLng());
		}
		ColorStation color = new ColorStation(idLine);
		rectOptions.color(Color.parseColor(color.toString()));
		rectOptions.width(3);
		_gMap.addPolyline(rectOptions);	
		
	}
}
